package com.zy.kotlinmvplib

import android.annotation.SuppressLint
import com.zy.kotlinbaselibrary.log.XLog
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import java.util.concurrent.TimeUnit

class MainPresenter: MainContract.Presenter(){
    override fun goData1() {
       mView?.let { view: MainContract.View ->
           mModel.getData1().compose(view.bindToLifecycle())
               .subscribe({
                   XLog.i("成功: ${it.getIncludeNull()?.toString()}")
               },{
                   XLog.i("失败: ${it.message}")
               },{
                   XLog.i("完成")
               })
       }
    }

    override fun goData() {
        mView?.let { mainView ->
            mModel.getData().compose(mainView.bindToLifecycle()).subscribe({
                XLog.i("成功: ${it.get().toString()}")
            },
                {
                    XLog.i("失败: ${it.message}")
                },
                {
                    XLog.i("完成")
                })
        }
    }

    override fun createModel(): MainModel {
        return MainModel()
    }

    @SuppressLint("CheckResult")
    fun testRxJava() {
        Observable.interval(1, TimeUnit.SECONDS)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .compose(mView?.bindToLifecycle())
            .subscribe({
                XLog.d("收到 $it")

            },{
                mView?.onRequestError("报错了")
            },{
                mView?.onRequestError("完了")
            })








//        Observable.create(object : Observable.OnSubscribe<String>(), ObservableOnSubscribe<Any> {
//            fun call(subscriber: Subscriber<in String>) {
//                var i = 0
//                while (i < 1000000000) {
//                    i++
//                }
//                subscriber.onNext(i.toString())
//                subscriber.onCompleted()
//            }
//        }).compose(this.bindUntilEvent<String>(ActivityEvent.PAUSE))
//            .subscribeOn(Schedulers.io())
//            .observeOn(AndroidSchedulers.mainThread())
//            .subscribe(object : Action1<String>() {
//                fun call(s: String) {
//                    mTextView.setText(s)
//                }
//            })

    }

}